package com.quanshu.exchange.ui.gui.message.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.chat.ChatConstant;
import com.quanshu.exchange.support.db.GroupDao;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.DateUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;
import com.quanshu.exchange.ui.gui.message.chat.ChatActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的群组
 * Created by xiniu003 on 2017/12/7.
 */

public class MyGroupsActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    //    @BindView(R.id.search_group)
//    ImageView searchGroup;
    @BindView(R.id.group_recycler_view)
    RecyclerView groupRecyclerView;
    private SingleAdapter<GroupEntity> adapter;
    private List<GroupEntity> datas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mygroups);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
//        RxViewHelper.getInstance().clicks(searchGroup, aVoid -> goSearch());
    }

    private void initView() {
        groupRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleAdapter(new DataDisplayer<GroupEntity>(this, R.layout.item_group_layout) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper wrapper, GroupEntity item, int position) {
                holder.setRoundImageUrl(R.id.group_avatar, item.getGroup_photo(), R.drawable.default_group_avatar);
                holder.setText(R.id.group_name, item.getGroup_name());
                holder.setText(R.id.group_desc, item.getGroup_introduce());
                holder.setText(R.id.group_created_time, "创建日期 ：" + DateUtil.TimeStamp2Date(item.getAdd_time() + "", "yyyy-MM-dd"));
                holder.setVisibility(R.id.group_is_added, View.GONE);
                if (item.getOwner().equals(currentUser.getMemberid())) {
                    holder.setVisibility(R.id.avatar_crown, View.VISIBLE);
                } else {
                    holder.setVisibility(R.id.avatar_crown, View.GONE);
                }
                holder.setOnClickListener(view ->
                        goGroupChatDetailActivity(item));
            }
        });
        groupRecyclerView.setAdapter(adapter);
    }

    private void initData() {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .getMyGroups(AppUtils.getSignParams(currentUser.getMemberid()), currentUser.getMemberid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<GroupEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<GroupEntity>> response) {
                        if (response.getInfo() != null) {
                            datas = response.getInfo();
                            adapter.setData(datas);
                            GroupDao.getInstance().save(datas);
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void goGroupChatDetailActivity(GroupEntity groupEntity) {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .getGroupInfo(AppUtils.getSignParams(currentUser.getMemberid(), groupEntity.getHx_groupid()),
                        currentUser.getMemberid(), groupEntity.getHx_groupid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<GroupEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<GroupEntity> response) {
                        GroupEntity groupEntity = response.getInfo();
                        GroupDao.getInstance().save(groupEntity);
                        Intent intent = new Intent(MyGroupsActivity.this, ChatActivity.class);
                        intent.putExtra("CHAT_TYPE", ChatConstant.CHAT_TYPE_GROUP);
                        intent.putExtra("conversationID", groupEntity.getHx_groupid());
                        startActivity(intent);
                    }
                });
    }

}
